﻿using hegang_winform.dao;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace hegang_winform.services.impl
{
    class TaskAtZero : FixedTimeTaskObject
    {
        private static DbObject o= (DbObject)Assembly.Load("hegang_winform").CreateInstance(ConfigurationManager.AppSettings["DB"]);

        public void executeTask()
        {
            DateTime now = DateTime.Now;
            //设置任务启动时间
            double hour = 23.0;     //小时   
            double minute = 50.0;   //分钟
            DateTime startTime = DateTime.Today.AddHours(hour).AddMinutes(minute);
            if (now > startTime)
            {
                startTime = startTime.AddDays(1.0);
            }
            int delay = (int)((startTime - now).TotalMilliseconds);
            var t = new System.Threading.Timer(handle_event);
            //设置线程参数 任务delay毫秒后启动
            t.Change(delay, Timeout.Infinite);
        }

        private void handle_event(object state)
        {
            #region 执行的任务
            task1();
            task2();
            task3();
            task4();
            #endregion

            // 再次设定任务执行时间
            executeTask();
        }

        /// <summary>
        /// 统计主井和副井的提升机状态表
        /// </summary>
        private void task1()
        {
            string mtsjgd_live_SQL = string.Format("" +
            "CREATE TABLE mtsjzt_live_tmp LIKE mtsjzt_live;" +
            "INSERT INTO mtsjzt_live_tmp SELECT * FROM mtsjzt_live;" +
            "INSERT INTO " +
                "mtsjtj(TM,YS,JX,JJKC,XZ,GZ,RWTZ,TimeStamp)" +
            "VALUES(" +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE TM = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE YS = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE JX = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE JJKC = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE XZ = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE GZ = '1')," +
                "(SELECT COUNT(id) FROM mtsjzt_live_tmp WHERE RWTZ = '1')," +
                "'{0}');" +
            "DROP TABLE mtsjzt_live_tmp ;" +
            "TRUNCATE mtsjzt_live;", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            string ftsjgd_live_SQL = string.Format("" +
            "CREATE TABLE ftsjzt_live_tmp LIKE ftsjzt_live;" +
            "INSERT INTO ftsjzt_live_tmp SELECT * FROM ftsjzt_live;" +
            "INSERT INTO " +
                "ftsjtj(TR,TW,JX,JJKC,XZ,GZ,RWTZ,TimeStamp)" +
            "VALUES(" +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE TR = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE TW = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE JX = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE JJKC = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE XZ = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE GZ = '1')," +
                "(SELECT COUNT(id) FROM ftsjzt_live_tmp WHERE RWTZ = '1')," +
                "'{0}');" +
            "DROP TABLE ftsjzt_live_tmp ;" +
            "TRUNCATE ftsjzt_live;", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            o.cmdNoReturn(mtsjgd_live_SQL);
            o.cmdNoReturn(ftsjgd_live_SQL);
        }

        /// <summary>
        /// 每天统计主井的勾数和电度
        /// </summary>
        private void task2()
        {
            int GS_sum;
            int DD_sum;

            DateTime now = DateTime.Now;
            string beginDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");

            /*if ((now - DateTime.Today).TotalHours < 23)
            {
                beginDate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                beginDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
            }*/

            string str = string.Format("SELECT COUNT(id) FROM mtsjgd_live WHERE TimeStamp > '{0}'",beginDate);
            DataTable dt = o.cmdHasReturn(str);
            if (Convert.ToInt32(dt.Rows[0][0]) == 0)
            {
                GS_sum = 0;
                DD_sum = 0;
            }
            else
            {
                str = string.Format("SELECT SUM(GS),SUM(DD) FROM mtsjgd_live WHERE TimeStamp > '{0}'", beginDate);
                dt = o.cmdHasReturn(str);
                GS_sum = Convert.ToInt32(dt.Rows[0][0]);
                DD_sum = Convert.ToInt32(dt.Rows[0][1]);
            }

            //将统计结果存入数据库
            str = string.Format("" +
                "INSERT INTO " +
                    "mtsjgdtj(GS, DD, TimeStamp) " +
                "VALUES" +
                    "('{0}', '{1}', '{2}');", GS_sum, DD_sum, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            o.cmdNoReturn(str);

            //统计完后，清空mtsjgd_live
            //str = "TRUNCATE mtsjgd_live;";
            //o.cmdNoReturn(str);
        }

        /// <summary>
        /// 每天统计副井的勾数和电度
        /// </summary>
        public void task3()
        {
            int GS_sum;
            int DD_sum;

            DateTime now = DateTime.Now;
            string beginDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");

            /*if ((now - DateTime.Today).TotalHours < 23)
            {
                beginDate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                beginDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
            }*/

            string str = string.Format("SELECT COUNT(id) FROM ftsjgd_live WHERE TimeStamp > '{0}'", beginDate);

            DataTable dt = o.cmdHasReturn(str);
            if (Convert.ToInt32(dt.Rows[0][0]) == 0)
            {
                GS_sum = 0;
                DD_sum = 0;
            }
            else
            {
                str = string.Format("SELECT SUM(GS),SUM(DD) FROM ftsjgd_live WHERE TimeStamp > '{0}'", beginDate);
                dt = o.cmdHasReturn(str); 
                GS_sum = Convert.ToInt32(dt.Rows[0][0]);
                DD_sum = Convert.ToInt32(dt.Rows[0][1]);
            }

            //将统计结果存入数据库
            str = string.Format("" +
                "INSERT INTO " +
                    "ftsjgdtj(GS, DD, TimeStamp) " +
                "VALUES" +
                    "('{0}', '{1}', '{2}');", GS_sum, DD_sum, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            o.cmdNoReturn(str);

            //统计完后，清空ftsjgd_live
            //str = "TRUNCATE ftsjgd_live;";
            //o.cmdNoReturn(str);
        }

        /// <summary>
        /// 统计当日故障发生次数
        /// </summary>
        private void task4()
        {
            string time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            Console.WriteLine(time);
            string str = string.Format("SELECT COUNT(id) FROM gz WHERE timestamp>'{0}';", time);
            DataTable dt = o.cmdHasReturn(str);
            string str_save = string.Format("INSERT INTO gztj(num, timestamp) VALUES('{0}','{1}');", dt.Rows[0][0], System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            o.cmdNoReturn(str_save);
        }
    }
}
